﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Reflection;
using System.Web.Mvc;

namespace CNRegex.Web.Utility
{
	public class PaginateHelper
	{
		public int PageSize { get; private set; }
		public int PageNo { get; private set; }
		public int TotalCount { get; private set; }
		public int TotalPages { get; private set; }
		public string Url { get; set; }
		public bool IsHasAnd { get; private set; }
		public int PixCount { get; private set; }
		public string EmptyTip { get; set; }
		public string ParaPageIndex { get; set; }
		/// <summary>
		/// 搜索对象名称
		/// </summary>
		public string SearchClassObjName { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageSize"></param>
		/// <param name="totalCount"></param>
		/// <param name="pageIndex"></param>
		/// <param name="url"></param>
		public PaginateHelper(int pageSize, int totalCount, int pageIndex, string url)
			: this(pageSize, totalCount, pageIndex, 5, url) { }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageSize"></param>
		/// <param name="totalCount"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pixCount"></param>
		/// <param name="url"></param>
		private PaginateHelper(int pageSize, int totalCount, int pageIndex, int pixCount, string url)
		{
			Url = url;
			PageNo = pageIndex;
			PageSize = pageSize;
			TotalCount = totalCount;
			TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
			TotalCount = totalCount;
			PixCount = pixCount;
		}

		public bool HasPreviousPage
		{
			get { return PageNo > 0; }
		}
		public bool HasNextPage
		{
			get { return PageNo + 1 < TotalPages; }
		}

		public static PaginateHelper ConstructPaginate(int pageSize, int totalCount, int pageIndex, string url, object searchClassObj = null, string searchClassObjName= null)
		{
			if (!url.EndsWith("/"))
				url += "/?";
			else
				url += "?";
			PaginateHelper pHelper = new PaginateHelper(pageSize, totalCount, pageIndex, url);
			pHelper.SearchClassObjName = searchClassObjName;
			pHelper.Url = url + pHelper.ConstructSearchCondition(searchClassObj);
			return pHelper;
		}

		private string ConstructSearchCondition(object searchClassObj)
		{
			if (searchClassObj == null)
				return "";
			SearchClassObjName = string.IsNullOrEmpty(SearchClassObjName) ? "" : SearchClassObjName;
			StringBuilder sb = new StringBuilder();
			Type searchClassType = searchClassObj.GetType();
			PropertyInfo[] propertyInfos = null;
			propertyInfos = searchClassType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
			foreach (var item in propertyInfos)
			{
				object propertyValue = item.GetValue(searchClassObj, null);
				if (Attribute.GetCustomAttribute(item, typeof(PaginateNotComposeAttribute)) != null || propertyValue == null || string.IsNullOrEmpty(propertyValue.ToString()))
					continue;
				if (propertyValue.GetType() == typeof(DateTime))
					propertyValue = ((DateTime)propertyValue).ToString("yyyy-MM-dd");
				sb.Append(string.Format("{0}.{1}={2}", SearchClassObjName, item.Name, HttpUtility.UrlEncode(propertyValue.ToString())));
				sb.Append("&");
			}

			string temp = sb.ToString();
			if (string.IsNullOrEmpty(temp))
				return "";
			return temp;

		}

		/// <summary>
		/// 多少条每页
		/// </summary>
		/// <returns></returns>
		public static SelectList ListPageSizeOption()
		{
			List<SelectListItem> list = new List<SelectListItem>() { 
				 new SelectListItem(){ Text="10", Value="10"},
				 new SelectListItem(){ Text="20", Value="20"},
				 new SelectListItem(){ Text="40", Value="40"},
				 new SelectListItem(){ Text="60", Value="60"},
				 new SelectListItem(){ Text="100", Value="100"},
				 new SelectListItem(){ Text="200", Value="200"},
			};
			return new SelectList(list, "Value", "Text");
		}
	}

	[AttributeUsage(AttributeTargets.Property)]
	public class PaginateNotComposeAttribute : System.Attribute
	{

	}
}
